/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2023-2023. All rights reserved.
 */

package com.huawei.api.utils;

import com.huawei.api.exception.BaseCustomerException;

import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Paths;
import java.util.Properties;

/**
 * 路径工具类
 *
 * @since 2023/10/28
 */
public class PathUtil {
    /**
     * 获取命令执行的路径
     *
     *
     * @return String 绝对路径
     */
    public static String getSystemPath(){
        return System.getProperty("user.dir");
    }

    public static String getEnv() throws BaseCustomerException {
        String env = "";
        try {
            InputStream inputStream = ClassLoader.getSystemResourceAsStream("config.properties");
            Properties properties = new Properties();
            properties.load(inputStream);
            env = properties.getProperty("tool.path");
        } catch (IOException exception) {
            throw new BaseCustomerException("get env path error");
        }
        return env;
    }
    public static String getAbsolutePath(String relativePath) {
        String currentWorkingDirectory = getSystemPath();
        String absolutePath;
        if (relativePath.startsWith("./") || relativePath.startsWith(".")) {
            relativePath = relativePath.substring(2);
        }
        absolutePath = Paths.get(currentWorkingDirectory, relativePath).toString();

        return absolutePath;
    }

    public static String getAbsolutePath(String rundir,String relativePath) {
        String currentWorkingDirectory = rundir;
        String absolutePath;
        if (relativePath.startsWith("./") || relativePath.startsWith(".")) {
            relativePath = relativePath.substring(2);
        }
        absolutePath = Paths.get(currentWorkingDirectory, relativePath).toString();

        return absolutePath;
    }
}
